package cn.tianya.irock.bll;

import java.util.List;

import cn.tianya.irock.cache.AppCache;
import cn.tianya.irock.cache.ICache;
import cn.tianya.irock.core.HttpRuntime;
import cn.tianya.irock.data.dalfactory.DataAccess;
import cn.tianya.irock.data.idal.IMethodDataProvider;
import cn.tianya.irock.enums.DataProviderAction;
import cn.tianya.irock.method.Method;

public class Methods {
	private static IMethodDataProvider provider;

	public static String GROUP_METHOD = "GROUP_METHOD";
	
	static {
		provider = DataAccess.createMethodProvider();
	}
	
	@SuppressWarnings("unchecked")
	public static List<Method> getMethods(String group) {
		String key = "methods:group:" + group;
		List<Method> methods = (List<Method>) HttpRuntime.cache().get(key);
		if (methods == null) {
			methods = provider.getMethods(group);
			AppCache.add(key, GROUP_METHOD, methods, ICache.MINUTE_FACTOR);
		}

		return methods;
	}
	
	public static void delete(int methodId) {		
	}
	
	public static void delete(String group) {
		provider.delete(group);
		AppCache.clear(GROUP_METHOD);
	}
	
	public static void update(List<Method> methods) {
		provider.updateDelete(methods, DataProviderAction.Update);
		AppCache.clear(GROUP_METHOD);
	}
	
	public static void add(String group, List<Method> methods) {
		provider.add(group, methods);
		AppCache.clear(GROUP_METHOD);
	}
}
